import ElementUI from 'element-ui';
import store from '@/store';
function initWebSocket(e) {
  const hostLO = document.location.host;
  const baseURL = process.env.VUE_APP_BASE_API;
  const host = baseURL.replace(/^https?\:\/\//i, '');
  const wsUri = `wss://${host ? host : hostLO}/ws`;
  console.log('WebSocket已连接', wsUri);
  this.socket = new WebSocket(wsUri, e); //这里面的this都指向vue
  this.socket.onerror = webSocketOnError;
  this.socket.onmessage = webSocketOnMessage;
  this.socket.onclose = closeWebsocket;
}
function webSocketOnError(e) {
  console.log('WebSocket连接发生错误');
  // ElementUI.Notification({
  //     title: '',
  //     message: "WebSocket连接发生错误",
  //     type: 'error',
  //     duration: 0,
  // });
}
function webSocketOnMessage(e) {
  store.commit('WEBSOCKET_DATA', e);
}
// 关闭websiocket
function closeWebsocket() {
  console.log('连接已关闭...');
}
function close() {
  this.socket.close(); // 关闭 websocket
  this.socket.onclose = function (e) {
    console.log(e); //监听关闭事件
    console.log('关闭');
  };
}
function webSocketSend(agentData) {
  this.socket.send(agentData);
}
export default {
  initWebSocket,
  close,
  webSocketOnMessage
};
